// #include "initilizeGlobals.js"
var theArticle;
var gUnescapeHTML;

function onKeyPress(e){
  switch (e.keyCode){
  case 37: // LEFT
    window.back();
  }
}
function onLoad(){
  theArticle = kb.sym(document.location.href);
  gUnescapeHTML = Cc["@mozilla.org/feed-unescapehtml;1"]
                  .getService(Ci.nsIScriptableUnescapeHTML);

  //fetch the data on this aritcle
  if (!isBBS){ //@ is it possible to have a uniform approach?
    var seeAlsos = kb.each(theArticle, RDFS('seeAlso'));
    for (var i = 0; i < seeAlsos.length; i++)
      sf.lookUpThing(seeAlsos[i]);
    sf.addCallback('done', initPost);
  }
  if (kb.any(theArticle, BBS('id_post_content'))||
      kb.any(theArticle, CONTENT('encoded'))){
    initPost(); //already loaded, no need to wait for any connection
  } else if (isBBS){
    bbsProtocolHandler.setCallback(document.location.href, initPost);
  }
  window.addEventListener('keypress', onKeyPress, false);
}
initPost.owner = window;
window.addEventListener("DOMContentLoaded", onLoad, false);

function resizePost(){
  var post_header = document.getElementById("id_post_header");
  var post_content = document.getElementById("id_post_content");
  post_content.style.top = '80px';
  post_content.style.bottom = '0px';
  post_content.scrollTop = 0;
}

function initPost(){
  resizePost();
  //screen.availHeight - document.getElementById("post_head").height;
  //var URI = document.location;
  //var URIstr = URI.href;
  //var URIstr = 'bbs://ptt2.cc/chaogold/1234567';
  var URIstr = document.location.href;
  //var URI_component = URIstr.split("\/");
  //var host_name = URI_component[2];
  //var board_name = URI_component[3];
  //var post_id = URI_component[4];
  //alert('host:' + host_name + "\nboard:" + board_name + "\nid:" + post_id);
  
  //RDFa properties declaration
  //應該是在資料庫API的javascript檔裡宣告好了  吧 嗎?
  var property_title = kb.sym("http://purl.org/dc/terms/title");
  //var property_author;
  var property_board = kb.sym("http://code.google.com/p/on-the-wave/voc#board_name");
  var property_id = kb.sym("http://code.google.com/p/on-the-wave/voc#author_id");
  //var property_name = kb.sym("http://code.google.com/p/on-the-wave/voc#author_nickname");
  var property_nickname = kb.sym("http://code.google.com/p/on-the-wave/voc#author_nickname");
  var property_time = kb.sym("http://purl.org/dc/terms/created");
  var property_post_content = kb.sym("http://code.google.com/p/on-the-wave/voc#id_post_content");
  
  //定義物件..其實我一直在想aru到底是什麼意思 XD
  var aru_article = kb.sym(URIstr);
  var aru_article_title = kb.the(aru_article, property_title);
  //var aru_author = kb.the(aru_article, property_author);
  //var aru_author_id = kb.any(aru_author, property_id);
  var aru_author_id = kb.any(aru_article, property_id);
  var aru_author_nickname = kb.any(aru_article, property_nickname);
  //var aru_article_time = kb.the(aru_article, property_time);
  var aru_article_board = kb.any(aru_article, property_board);
  //var aru_article_content = kb.the(aru_article, property_post_content);
  
  //把資料填入對應欄位
  document.getElementById('post_title').innerHTML = aru_article_title.value;
  document.title = aru_article_title.value;
  if (aru_article_board) //從信箱轉來的文章沒有"看板"!! (@處理界面)
    document.getElementById('board_name').innerHTML = aru_article_board.value;
  
  var maker = kb.any(aru_article, FOAF('maker'));
  if (maker){
    var label = kb.any(maker, RDFS('label')); //for WP SIOC Plugin
                                              // , not good @
    if (label)
      document.getElementById('author_id').innerHTML = label.value;
  } else if (aru_author_id) {
    document.getElementById('author_id').innerHTML = aru_author_id.value;
  }

  if (aru_author_nickname)
    document.getElementById('author_nickname').innerHTML = aru_author_nickname.value;

  var postTime = kb.any(aru_article, DCTERMS('created'));
  if (postTime)
    document.getElementById('post_time').innerHTML = postTime.value;

  var htmlContent = kb.any(aru_article, CONTENT('encoded')) ||
                    kb.any(aru_article, property_post_content);

  //document.getElementById('id_post_content').innerHTML = htmlContent.value;
  //element.innerHTML = "&nbsp;" would raise an NS_ERROR_INVALID_POINTER
  //if it is XHTML DOM API (also for HTML? don't know)
  //the following does the right thing
  var container = document.getElementById('id_post_content')
  var frag = gUnescapeHTML.parseFragment(htmlContent.value, false, null,
                                         container);
  container.appendChild(frag);
  
}

//Shortcut commands

function scrollDown(){
	var post_content = document.getElementById("id_post_content");
	post_content.scrollTop = post_content.scrollTop+24;
}

function scrollUp(){
	var post_content = document.getElementById("id_post_content");
	post_content.scrollTop = post_content.scrollTop>24 ? (post_content.scrollTop-24):0;
}

shortcut.add( "Down" , scrollDown );
shortcut.add( "Up" , scrollUp );
